/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmCliente.java
 *
 * Created on 05-12-2014, 10:15:19 AM
 */
package Presentacion;
import java.io.*;
import Negocio.Cliente;
import Negocio.ListaCliente;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Date;
import javax.swing.table.DefaultTableModel;
import Negocio.EscrituraFichero;
import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JFrame;
import java.awt.*;
/**
 *
 * @author Estudiante
 */
public class FrmCliente extends javax.swing.JFrame {

    /** Creates new form FrmCliente */
    public FrmCliente() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        txtid = new javax.swing.JTextField();
        btnCargar = new javax.swing.JButton();
        txtNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtApellidoPaterno = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtApellidoMaterno = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtFechaNacimiento = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtCarnet = new javax.swing.JTextField();
        btnSalir = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        rbMasculino = new javax.swing.JRadioButton();
        rbFemenino = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btnBuscar = new javax.swing.JButton();
        txtBuscarCliente = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        btnLeerFichero = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnguardar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("id");

        txtid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtidActionPerformed(evt);
            }
        });

        btnCargar.setText("Cargar");
        btnCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCargarActionPerformed(evt);
            }
        });

        jLabel2.setText("Nombre");

        jLabel3.setText("A.Paterno");

        jLabel4.setText("A.Materno");

        jLabel5.setText("Fecha Nacimiento");

        jLabel6.setText("Sexo");

        jLabel7.setText("Carnet");

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel8.setText("Registrar Cliente");

        buttonGroup1.add(rbMasculino);
        rbMasculino.setText("Masculino");

        buttonGroup1.add(rbFemenino);
        rbFemenino.setText("Femenino");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        jLabel9.setText("id");

        btnLeerFichero.setText("Leer Fichero");
        btnLeerFichero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLeerFicheroActionPerformed(evt);
            }
        });

        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnguardar.setText("guardar");
        btnguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnguardarActionPerformed(evt);
            }
        });

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(104, 104, 104)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnCargar, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnModificar))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(rbMasculino)
                                        .addGap(18, 18, 18)
                                        .addComponent(rbFemenino))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(txtFechaNacimiento, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(txtCarnet, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txtNombre, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtApellidoPaterno, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtApellidoMaterno, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnguardar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnLeerFichero, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(52, 52, 52)
                                .addComponent(jLabel9)
                                .addGap(18, 18, 18)
                                .addComponent(txtBuscarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(37, 37, 37)
                                .addComponent(btnBuscar))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(46, 46, 46))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtApellidoPaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtApellidoMaterno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFechaNacimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(rbMasculino)
                    .addComponent(rbFemenino))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCarnet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCargar)
                    .addComponent(btnSalir)
                    .addComponent(btnModificar))
                .addContainerGap(55, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnguardar)
                    .addComponent(btnEliminar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBuscar)
                    .addComponent(txtBuscarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(btnLeerFichero))
                .addGap(57, 57, 57)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
private void CargarObjetoCliente(Negocio.Cliente objcliente){
  objcliente.setId(Integer.parseInt(this.txtid.getText()));
     String  a =(this.txtid.getText());
        objcliente.setNombreCliente(this.txtNombre.getText());
        objcliente.setApellidoPaterno(this.txtApellidoPaterno.getText());
        objcliente.setApellidoMaterno(this.txtApellidoMaterno.getText());

        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String dateInString =this.txtFechaNacimiento.getText();
        try{
            Date date=formatter.parse(dateInString);
            objcliente.setFechaNacimiento(date);
        }
        catch (Exception e){}
        if (this.rbMasculino.isSelected()){
        objcliente.setSexo("Masculino");
        }
        else{
        objcliente.setSexo("Femenino");
        }

        objcliente.setCarnet(Integer.parseInt(this.txtCarnet.getText()));
        }
private void leerFichero(){
    String texto="";
      File archivo = null;
      FileReader fr = null;
      BufferedReader br = null;
 
      try {
         // Apertura del fichero y creacion de BufferedReader para poder
         // hacer una lectura comoda (disponer del metodo readLine()).
           int a=Integer.parseInt(this.txtid.getText());
           String b = "D:\\prueba\\prueba";
         //archivo = new File ("D:\\prueba\\prueba8.txt");
           
           //archivo = new File (b+a+"."+"txt");
           archivo = new File ("D:\\prueba\\prueba.txt");
         fr = new FileReader (archivo);
         br = new BufferedReader(fr);
 
         // Lectura del fichero
         String temporal="";
         String linea;
         while((linea=br.readLine())!=null){
         temporal=temporal+linea;
         System.out.println(linea);
         }
           
         texto=temporal;//System.out.println(linea);
      }
      catch(Exception e){
         e.printStackTrace();
      }finally{
         // En el finally cerramos el fichero, para asegurarnos
         // que se cierra tanto si todo va bien como si salta 
         // una excepcion.
         try{                    
            if( null != fr ){   
               fr.close();     
            }                  
         }catch (Exception e2){ 
            e2.printStackTrace();
         }
      }
   
}
private void escribirJava(){
    Negocio.Cliente objcliente=new Cliente();
  FileWriter fichero = null;
        PrintWriter pw = null;
        try
        {
            int a=Integer.parseInt(this.txtid.getText());
           String b = "D:\\prueba\\prueba";
            fichero = new FileWriter(b+a+"."+"txt");
            pw = new PrintWriter(fichero);
            //////////////////////////////////////
 if (this.rbMasculino.isSelected()){
        objcliente.setSexo("Masculino");
         pw.println(this.txtid.getText()+","+this.txtNombre.getText()+"," +this.txtApellidoPaterno.getText()+"," +this.txtApellidoMaterno.getText()+"," +this.txtFechaNacimiento.getText() +","+"M"+","+this.txtCarnet.getText());
        }
        else{
         pw.println(this.txtid.getText()+"," +this.txtNombre.getText()+"," +this.txtApellidoPaterno.getText()+"," +this.txtApellidoMaterno.getText()+","+this.txtFechaNacimiento.getText() +","+"F"+","+this.txtCarnet.getText()+","+"F");
        objcliente.setSexo("Femenino");
        /////////////////////////
        }
           // for (int i = 0; i < 1; i++)
                
               // pw.println(i+","+this.txtNombre.getText()+"," +this.txtApellidoPaterno.getText()+"," +this.txtApellidoMaterno.getText()+"," +this.txtid.getText() +","+this.txtFechaNacimiento.getText() +","+this.txtCarnet.getText());
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
           try {
           // Nuevamente aprovechamos el finally para 
           // asegurarnos que se cierra el fichero.
           if (null != fichero)
              fichero.close();
           } catch (Exception e2) {
              e2.printStackTrace();
           }
        }
}
        
    private void btnCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCargarActionPerformed
        // TODO add your handling code here:
        try{
       Negocio.ListaCliente L=new ListaCliente();
       Negocio.Cliente objcliente=new Cliente();
       Negocio.EscrituraFichero objescritura= new EscrituraFichero();
       this.CargarObjetoCliente(objcliente);
       this.escribirJava();
         L.Insertar(objcliente);
        }
        catch(Exception e4){e4.printStackTrace();}
         
         
       //------------------------------- guardar en ficghero 
         
         ///////////
         
         
       
    }//GEN-LAST:event_btnCargarActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        // TODO add your handling code here:
        
        Negocio.ListaCliente objLista=new Negocio.ListaCliente();
        List<Negocio.Cliente>objListaCliente=new ArrayList<Negocio.Cliente>();
        objListaCliente=objLista.TraerClientePorCarnet(Integer.parseInt(this.txtBuscarCliente.getText()));
        DefaultTableModel modelo =new DefaultTableModel();
        this.jTable1.setModel(modelo);
        modelo.addColumn("id");
        modelo.addColumn("NombreCliente");
        modelo.addColumn("ApellidoPaterno");
        modelo.addColumn("ApellidoMaterno");
        modelo.addColumn("FechaNacimiento");
        modelo.addColumn("Sexo");
        modelo.addColumn("Carnet");
         Iterator iterator=objListaCliente.listIterator();
         while(iterator.hasNext()){
          Negocio.Cliente objCliente=(Negocio.Cliente)iterator.next();
                  Object[] fila=new Object[7];
                  fila[0]=objCliente.getId();
                  fila[1]=objCliente.getNombreCliente();
                  fila[2]=objCliente.getApellidoPaterno();
                  fila[3]=objCliente.getApellidoMaterno();
                  fila[4]=objCliente.getFechaNacimiento();
                  fila[5]=objCliente.getSexo();
                  fila[6]=objCliente.getCarnet();
                  modelo.addRow(fila);
         }
    }//GEN-LAST:event_btnBuscarActionPerformed
public  void TableDemo(){
        // super("Ejemplo 3");
          

        DefaultTableModel myModel = new DefaultTableModel();
        myModel.addColumn("id");
        myModel.addColumn("Nombre");
        myModel.addColumn("AP");
        myModel.addColumn("AM");
        myModel.addColumn("fechanacimiento");
          myModel.addColumn("sexo");
            myModel.addColumn("carnet");
       
        //JTable table = new JTable(myModel);
        this.jTable1.setModel(myModel);
       // table.setPreferredScrollableViewportSize(new Dimension(500, 150));


         
            BufferedReader br = null;
                try{
                     int a=Integer.parseInt(this.txtid.getText());
                     String b = "D:\\prueba\\prueba";
                   // br = new BufferedReader(new FileReader("D:\\prueba\\prueba8.txt"));
                    br = new BufferedReader(new FileReader(b+a+"."+"txt"));
                     String line = br.readLine();

                    for(int row = 0; row < 10 ; row++){
                        for(int column = 0; column<7 ;column++){


                                   while (line != null )
                                   {
                                     String [] rowfields = line.split("\\,");
                                     myModel.addRow(rowfields);
                                     line = br.readLine();
                                    }
                                   
                                }
                             }

                        }
                catch(Exception e)
                  {
                            System.out.println(e.getMessage());
                  }
        

        

       /*/ JTabbedPane panelInformacion = new JTabbedPane();
        JPanel panel1 = new JPanel();
        panel1.add( new JScrollPane( table ));
        panelInformacion.addTab( "Codigo", null, panel1, "Primer panel" );
        //agregar objeto JTabbedPane al contenedor
        getContentPane().add( panelInformacion );

        setSize( 800, 600 );
        setVisible( true );/*/
    
}
public String LeerArchivo(String direccion){
String texto="";
try
{
    BufferedReader bf=new BufferedReader(new FileReader(direccion));
String temporal="";
String bfread;
while ((bfread=bf.readLine())!=null){
temporal=temporal+bfread;
}
texto=temporal;

}
catch(Exception e1){System.err.println("no se encontro el archivo");}
return texto;
        
}
    private void btnLeerFicheroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLeerFicheroActionPerformed
        // TODO add your handling code here:
        //this.leerFichero();
       this.TableDemo();
	//aplicacion.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        //this.cargartabla();
    }//GEN-LAST:event_btnLeerFicheroActionPerformed

    
    private void modificar(){
    
     for (int i = 0; i <  jTable1.getRowCount(); i++) {
         
            String id = "", Nombre = "", AP = "", AM = "", fechanacimento = "", sexo = "", carnet = "";
            
           txtid.setText(jTable1.getValueAt(jTable1.getSelectedRow(),0).toString());
            txtNombre.setText(jTable1.getValueAt(jTable1.getSelectedRow(),1).toString());
            
           // id= (String) jTable1.getValueAt(jTable1.getSelectedRow(), 0);
         //   Nombre = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 1);
            AP = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 2);
            AM = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 3);
            fechanacimento = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 4);
            sexo = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 5);
            carnet = (String) jTable1.getValueAt(jTable1.getSelectedRow(), 6);
          //  

           // txtid.setText(id);
          //  txtNombre.setText(Nombre);
            txtApellidoPaterno.setText(AP);
            txtApellidoMaterno.setText(AM);
            txtFechaNacimiento.setText(fechanacimento);
            txtCarnet.setText(carnet);           
           
        
    }          
    }
    ///////////////-----------------
    
    
   
  
    ///////////////////-------------
    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        // TODO add your handling code here:
           // txtid.setText(jTable1.getValueAt(jTable1.getSelectedRow(),0).toString());
            //txtNombre.setText(jTable1.getValueAt(jTable1.getSelectedRow(),1).toString());
        this.modificar();
         
        //this.mostrar(WIDTH, WIDTH, null);
        
    }//GEN-LAST:event_btnModificarActionPerformed
/////////////////***********************
    private void escribirJava1(){
    Negocio.Cliente objcliente=new Cliente();
  FileWriter fichero = null;
        PrintWriter pw = null;
        try
        {
            int a=Integer.parseInt(this.txtid.getText());
           String b = "D:\\prueba\\prueba";
            //fichero = new FileWriter(b+a+"."+"txt");
            fichero = new FileWriter("D:\\prueba\\prueba.txt");
            pw = new PrintWriter(fichero);
            //////////////////////////////////////
            
            String it;
           
 if (this.rbMasculino.isSelected()){
        objcliente.setSexo("Masculino");
         pw.println(this.txtid.getText()+","+this.txtNombre.getText()+"," +this.txtApellidoPaterno.getText()+"," +this.txtApellidoMaterno.getText()+"," +this.txtFechaNacimiento.getText() +","+"M"+","+this.txtCarnet.getText());
        }
        else{
         pw.println(this.txtid.getText()+"," +this.txtNombre.getText()+"," +this.txtApellidoPaterno.getText()+"," +this.txtApellidoMaterno.getText()+","+this.txtFechaNacimiento.getText() +","+"F"+","+this.txtCarnet.getText()+","+"F");
        objcliente.setSexo("Femenino");
 
        /////////////////////////
        }
           // for (int i = 0; i < 1; i++)
                
               // pw.println(i+","+this.txtNombre.getText()+"," +this.txtApellidoPaterno.getText()+"," +this.txtApellidoMaterno.getText()+"," +this.txtid.getText() +","+this.txtFechaNacimiento.getText() +","+this.txtCarnet.getText());
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
           try {
           // Nuevamente aprovechamos el finally para 
           // asegurarnos que se cierra el fichero.
           if (null != fichero)
              fichero.close();
           } catch (Exception e2) {
              e2.printStackTrace();
           }
        }
}
        
    /////////////////////////*********************
    private void btnguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnguardarActionPerformed
        // TODO add your handling code here:
         try{
       Negocio.ListaCliente L=new ListaCliente();
       Negocio.Cliente objcliente=new Cliente();
       Negocio.EscrituraFichero objescritura= new EscrituraFichero();
       this.CargarObjetoCliente(objcliente);
       this.escribirJava1();
         L.Insertar(objcliente);
        }
        catch(Exception e4){e4.printStackTrace();}
    }//GEN-LAST:event_btnguardarActionPerformed

    private void eliminar(){
     Negocio.Cliente objcliente=new Cliente();
  FileWriter fichero = null;
        PrintWriter pw = null;
        try
        {
            int a=Integer.parseInt(this.txtid.getText());
           String b = "D:\\prueba\\prueba";
            //fichero = new FileWriter(b+a+"."+"txt");
            fichero = new FileWriter("D:\\prueba\\prueba.txt");
            pw = new PrintWriter(fichero);
            //////////////////////////////////////
          
 
        /////////////////////////
        
           // for (int i = 0; i < 1; i++)
                
               // pw.println(i+","+this.txtNombre.getText()+"," +this.txtApellidoPaterno.getText()+"," +this.txtApellidoMaterno.getText()+"," +this.txtid.getText() +","+this.txtFechaNacimiento.getText() +","+this.txtCarnet.getText());
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
           try {
           // Nuevamente aprovechamos el finally para 
           // asegurarnos que se cierra el fichero.
           if (null != fichero)
              fichero.close();
           } catch (Exception e2) {
              e2.printStackTrace();
           }
        }
}
    
    
    }
    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void txtidActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtidActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtidActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
       
        
        //
        TableDemo aplicacion = new TableDemo();
	aplicacion.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        
        //
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FrmCliente().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCargar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnLeerFichero;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnguardar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JRadioButton rbFemenino;
    private javax.swing.JRadioButton rbMasculino;
    private javax.swing.JTextField txtApellidoMaterno;
    private javax.swing.JTextField txtApellidoPaterno;
    private javax.swing.JTextField txtBuscarCliente;
    private javax.swing.JTextField txtCarnet;
    private javax.swing.JTextField txtFechaNacimiento;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtid;
    // End of variables declaration//GEN-END:variables

    private void desbloquear() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
